#include <sstream>

#include "ServerMenuState.h"
#include "MainMenuState.h"
#include "LobbyState.h"

ServerMenuState::ServerMenuState()
        : MenuState("Server Menu"), numberOfPlayers(0), noOfplayersText(NULL), wellSizeText(NULL), wellSizePos(0)
{
    wellSizes.push_back("S");
    wellSizes.push_back("M");
    wellSizes.push_back("L");
}

void ServerMenuState::addMenuLines()
{
    addLine("1. Start server");
    addLine("2. Change number of players");
    addLine("3. Well Size");
    addLine("ESC. Back to main menu");
    addLine("");
    noOfplayersText = addLine("");
    wellSizeText = addLine("");
    performMenuAction2();
    performMenuAction3();
}

GameState* ServerMenuState::performMenuAction1()
{
    double wellSizeFactor = (2*wellSizePos + 8) / 10.0;
    return goToNextState(new LobbyState(numberOfPlayers, wellSizeFactor));
}

GameState* ServerMenuState::performMenuAction2()
{
    ++numberOfPlayers;
    if (numberOfPlayers > 4)
    {
        numberOfPlayers = 1;
    }
    std::ostringstream infoString;
    infoString << "Number of players: " << numberOfPlayers;
    noOfplayersText->setText(infoString.str());
    return this;
}

GameState* ServerMenuState::performMenuAction3()
{
    wellSizePos++;
    if (wellSizePos >= wellSizes.size())
    {
        wellSizePos = 0;
    }
    std::ostringstream infoString;
    infoString << "Well size: " << wellSizes[wellSizePos];
    wellSizeText->setText(infoString.str());
    return this;
}

GameState* ServerMenuState::performMenuActionEscape()
{
    return goToNextState(new MainMenuState());
}

ServerMenuState::~ServerMenuState()
{
}
